package com.cyy.hardwarecoding.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.cyy.hardwarecoding.R;
import com.cyy.hardwarecoding.ui.adapter.ActivityListAdapater;
import com.cyy.hardwarecoding.ui.adapter.RecyclerViewSimpleAdapter;
import com.orhanobut.logger.Logger;


import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.ls_activity) RecyclerView ls_activity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        bindData();
    }

    private void bindData() {
        List<String> activitysName = getAllActivitysName();
        ls_activity.setAdapter(new ActivityListAdapater(activitysName, new RecyclerViewSimpleAdapter.OnItemClickListener<String>() {
            @Override
            public void onClick(View view, int position, String data) {
                Context context = view.getContext();
                try {
                    Class clazz = Class.forName(data);
                    Intent intent = new Intent(context, clazz);
                    context.startActivity(intent);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }));
    }

    private void initView(){
        ButterKnife.bind(this);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getBaseContext(),
                LinearLayoutManager.VERTICAL, false);
        ls_activity.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        ls_activity.setLayoutManager(layoutManager);
    }

    private List<String> getAllActivitysName(){
        PackageManager pm = getPackageManager();
        String app_name = getPackageName();
        PackageInfo packageInfo = null;
        ArrayList<String> activitysName = null;
        try {
            packageInfo = pm.getPackageInfo(app_name, PackageManager.GET_ACTIVITIES);
            if(packageInfo != null){
                activitysName = new ArrayList<>(packageInfo.activities.length);
                for (ActivityInfo activityInfo: packageInfo.activities) {
                    if(activityInfo.name.startsWith(getPackageName())
                            && !activityInfo.name.equals(MainActivity.class.getName()) ){
                        activitysName.add(activityInfo.name);
                    }
                }
            }
        } catch (PackageManager.NameNotFoundException e) {
            Logger.e("没有找到该包名");
            e.printStackTrace();
        }
        return activitysName;
    }
}
